log.chalk   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
import logger from "fancy-log";
2
import chalk from "chalk";
3
4
class log {
5
  constructor(...arg: string[] | any | null) {
6
    if (arguments.length) {
7
      log.log(arguments);
8
    }
9
  }
10
  /**
11
   * random array
12
   * @param items
13
   */
14
  private static rand(items: any[]) {
15
    return items[Math.floor(Math.random() * items.length)];
16
  }
17
  /**
18
   * return color by their type
19
   * @param any
20
   */
21
  static type(any: any) {
22
    const type = typeof any;
23
    if (type == "undefined") return log.error(any);
24
    if (type == "string") return log.string(any);
25
    if (type == "number" || type == "bigint") return log.number(any);
26
    if (type == "function") return log.chalk().magentaBright(any);
27
    if (type == "boolean") {
28
      if (!any) {
29
        return log.error(any);
30
      } else {
31
        return log.success(any);
32
      }
33
    }
34
    if (type == "object") return log.chalk().cyan(any);
35
    if (type == "symbol") return log.chalk().hex("#DC143C")(any);
36
  }
37
  static string(msg: any) {
38
    return log.hex(log.rand(["#FF8C00", "#FFA500", "#FF7F50"]), msg);
39
  }
40
  static number(msg: any) {
41
    return log.hex(
42
      log.rand(["#7CFC00", "#7FFF00", "#ADFF2F", "#808000", "#98FB98"]),
43
      msg
44
    );
45
  }
46
  /**
47
   * Chalk instance
48
   */
49
  static chalk(): chalk.Chalk {
50
    return chalk;
51
  }
52
  /**
53
   * return greenBright color
54
   * @param msg
55
   */
56
  static success(msg: string) {
57
    return chalk.greenBright(msg);
58
  }
59
  /**
60
   * return redBright color
61
   * @param msg
62
   */
63
  static error(msg: string) {
64
    return chalk.redBright(msg);
65
  }
66
  /**
67
   * Clear console
68
   */
69
  static clear() {
70
    return console.clear();
71
  }
72
  /**
73
   * Generate Random Hex Color
74
   */
75
  static hexColor() {
76
    return Math.floor(Math.random() * 16777215).toString(16);
77
  }
78
  /**
79
   * Random Color
80
   * @param msg
81
   */
82
  static random(msg: string) {
83
    return this.hex(`#${this.hexColor()}`, msg);
84
  }
85
  /**
86
   * Output log custom hex color
87
   * @param hex hex color
88
   * @param msg message to output
89
   */
90
  static hex(hex: string, msg: string) {
91
    return chalk.hex(hex)(msg);
92
  }
93
  /**
94
   * Indicator rainbow
95
   */
96
  static enable_rainbow: boolean = false;
97
  static rainbow = function (want: boolean) {
98
    log.enable_rainbow = want;
99
  };
100
  /**
101
   * console.log
102
   */
103
  static log(...arg: any[]) {
104
    if (arguments.length) {
105
      var result = [];
106
      for (const key in arguments) {
107
        if (arguments.hasOwnProperty(key)) {
108
          var args = log.prettyprint(arguments[key]);
109
          result.push(args);
110
        }
111
      }
112
      logger(result.join(", "));
113
    }
114
  }
115
116
  static prettyprint(args: any) {
117
    if (typeof args == "boolean") {
118
      if (args) {
119
        args = chalk.greenBright(args);
120
      } else {
121
        args = chalk.redBright(args);
122
      }
123
    } else if (typeof args == "string") {
124
      args = chalk.hex("#c4750e")(args);
125
    } else if (typeof args == "object" || Array.isArray(args)) {
126
      args = this.prettyprint(JSON.stringify(args, null, 2));
127
    } else if (typeof args == "undefined") {
128
      args = chalk.red("undefined");
129
    }
130
    return args;
131
  }
132
}
133
134
Object.assign(log, chalk);
135
136
export = log;
137